//
//  AddStudentViewController.m
//  08-Homework
//
//  Created by dllo on 16/7/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AddStudentViewController.h"
#import "Student.h"

@interface AddStudentViewController ()
@property (nonatomic, strong) Student *student;
@property (nonatomic, strong) UITextField *nameTextField;
@property (nonatomic, strong) UITextField *genderTextField;
@property (nonatomic, strong) UITextField *ageTextField;
@property (nonatomic, strong) UITextField *phoneTextField;

@end

@implementation AddStudentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatSubViews];
}

- (void)creatSubViews {
    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
    _nameTextField.placeholder = @"输入姓名";
    [self.view addSubview:_nameTextField];
    
    self.genderTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 100, 50)];
    _genderTextField.placeholder = @"输入性别";
    [self.view addSubview:_genderTextField];
    
    self.ageTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];
    _ageTextField.placeholder = @"输入年龄";
    [self.view addSubview:_ageTextField];
    
    self.phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 250, 100, 50)];
    _phoneTextField.placeholder = @"输入电话号码";
    [self.view addSubview:_phoneTextField];
    
    UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    saveButton.frame = CGRectMake(100, 300, 50, 50);
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    [saveButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:saveButton];
    [saveButton addTarget:self action:@selector(saveStudent:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)saveStudent:(UIButton *)button {
    self.student = [[Student alloc] init];
    _student.name = _nameTextField.text;
    _student.gender = _genderTextField.text;
    _student.age = [NSNumber numberWithFloat:[_ageTextField.text floatValue]];
    _student.phoneNumber = _phoneTextField.text;
    if (self.delegate && [self.delegate respondsToSelector:@selector(addStudent:)] && ![_student.name isEqualToString:@""]) {
        [self.delegate addStudent:_student];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
